const express = require('express');
const path = require('path');
const session = require('express-session');
const config = require('./config')
const app = express();
const favicon = require('serve-favicon');
global.INIT_DIR=__dirname;
global.UPLOAD_DIR = path.join(__dirname, './uploads');
global.UPLOAD_TMP_DIR = path.join(__dirname, './upload_tmp');
// 静态资源服务
app.use(express.static('public'));
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true }));
// 设置模板引擎
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Session配置
app.use(session({
  secret: global.secret_key,
  resave: false,
  saveUninitialized: true
}));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
const indexRouter = require('./routes/index');
const adminRouter = require('./routes/admin/admin');
app.use('/', indexRouter);
app.use('/admin', adminRouter);
// 在app.js中添加Socket.IO支持
const server = require('http').createServer(app);
const io = require('socket.io')(server);

// 在路由中添加Socket.IO处理
io.on('connection', (socket) => {
  socket.on('uploadProgress', (data) => {
    io.emit('progressUpdate', data);
  });
});

// 修改启动方式
server.listen(global.server_port, () => {
  console.log(`Server running on port ${global.server_port}`);
});